/**
 * accordion - jQuery xui
 *
 * Licensed under the Apache v2
 *
 * Copyright 2015 xjb [ beymy.en@gmail.com ]
 *
 */
(function($) {
	
	function bindEvents(target) {
		var t = $(target);
		var state = $.data(target, 'accordion');
		var opts = state.options;
		
		t.find('>div.accordion-group>div.accordion-heading>a.accordion-toggle').unbind('.accordion').bind('click.accordion', function(){
			var bId = $(this).attr('href');
			var b = $(bId);
			
			if(b.hasClass('in')) {
				b.removeClass('in');
			} else {
				b.addClass('in');
			}
			
			if(!opts.multiple) {
				t.find('>div.accordion-group>div.accordion-body').not(b).removeClass('in');
			}
		});
	}
	
    $.fn.accordion = function(options, param) {
        if (typeof options == 'string') {
            return $.fn.accordion.methods[options](this, param);
        }
        options = options || {};
        return this.each(function() {
            var opts;
            var state = $.data(this, 'accordion');
            if (state) {
                opts = $.extend(state.options, options);
            } else {
                opts = $.extend({}, $.fn.accordion.defaults, $.fn.accordion.parseOptions(this), options);
                $.data(this, 'accordion', {
                    options: opts
                });
            }
            
            bindEvents(this);
        });
    };

    $.fn.accordion.methods = {
        options: function(jq) {
            return $.data(jq[0], 'accordion').options;
        }        
    };

    $.fn.accordion.parseOptions = function(target) {
        return $.extend({}, $.parser.parseOptions(target));
    };

    $.fn.accordion.defaults = {
        multiple: true
    };

})(jQuery);